using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Data;
using System.Data.Entity.Infrastructure;
using ShopPowerTemplate.Services.Properties;
using ShopPowerTemplate.Entities;

namespace ShopPowerTemplate.Services
{
    public class OrderServiceBase : Service
    {
		public OrderServiceBase(IEntitiesDB entitiesDB) : base(entitiesDB) { }
		
        public virtual Int32 Add(Order order)
        {
            order.Customer = EntitiesDB.EntryWithState(order.Customer, EntityState.Unchanged);
            order.OrderState = EntitiesDB.EntryWithState(order.OrderState, EntityState.Unchanged);
            EntitiesDB.OrderSet.Add(order);
            EntitiesDB.SaveChanges();
            return order.Id;
        }

        public virtual void Change(Order order)
        {
            order.Customer = EntitiesDB.EntryWithState(order.Customer, EntityState.Unchanged);
            order.OrderState = EntitiesDB.EntryWithState(order.OrderState, EntityState.Unchanged);
		    EntitiesDB.Entry(order).State = EntityState.Modified;

            EntitiesDB.SaveChanges();
        }
		
		public virtual void Remove(int id)
        {
			Order order = Find(id);
            try
            {
                EntitiesDB.OrderSet.Remove(order);
                EntitiesDB.SaveChanges();
            }
            catch (DbUpdateException)
            {
                throw new RemoveException(string.Format(Resources.RemoveException, order.ToString()));
            }
        }

        public virtual void Remove(Order order)
        {
			Remove(order.Id);
        }        

        public virtual Order Find(int id)
        {
			Order order = 
				EntitiesDB.OrderSet
					.Include("Customer")
					.Include("OrderState")
					.Include("OrderDetails")
					.FirstOrDefault(x => x.Id == id);
			
			if(order!=null)
			{
				var customerAux = order.Customer; //EF4.1 bug hack
				var orderStateAux = order.OrderState; //EF4.1 bug hack
			}
			return order;
        }

        public virtual List<Order> FindAll()
        {
            return EntitiesDB.OrderSet.OrderBy(x => x.Reference).ToList();
        }
		
		public virtual List<Order> Search(string searchValue)
        {
            return Search(searchValue, string.Empty, true, 0, 0);
        }

        public virtual List<Order> Search(string searchValue, int pageIndex, int pageSize)
        {
            return Search(searchValue, string.Empty, true, pageIndex, pageSize);
        }

        public virtual List<Order> Search(string searchValue, string orderBy)
        {
            return Search(searchValue, orderBy, true, 0, 0);
        }

        public virtual List<Order> Search(string searchValue, string orderBy, int pageIndex, int pageSize)
        {
            return Search(searchValue, orderBy, true, pageIndex, pageSize);
        }

        public virtual List<Order> Search(string searchValue, string orderBy = "", bool ascending = true, int pageIndex = 0, int pageSize = 10)
        {
            IQueryable<Order> query = EntitiesDB.OrderSet.Where(x => string.IsNullOrEmpty(searchValue) || x.Reference.Contains(searchValue));
			
			if(orderBy == string.Empty)
				orderBy = "Id";			
			
			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }
		
		public virtual List<Order> Search(Expression<Func<Order, bool>> searchCriteria, string orderBy = "", bool ascending = true, int pageIndex = 0, int pageSize=10)
        {
            IQueryable<Order> query = EntitiesDB.OrderSet.Where(searchCriteria);
            
			if(orderBy == string.Empty)
				orderBy = "Id";			
			
			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }

        public virtual int SearchCount(string searchValue)
        {
            return EntitiesDB.OrderSet.Where(x => string.IsNullOrEmpty(searchValue) || x.Reference.Contains(searchValue)).Count();
        }
		
		public virtual List<Order> FindAvailableOrderList<TKey>(List<Order> notAvailableList, string searchValue, System.Linq.Expressions.Expression<Func<Order, TKey>> orderBy, bool ascending, int pageIndex, int pageSize)
        {
            IEnumerable<int> notAvailableIDList = notAvailableList.Select(na => na.Id);
            IQueryable<Order> query = EntitiesDB.OrderSet.Where(a => !notAvailableIDList.Any(id => id == a.Id)).Where(x => string.IsNullOrEmpty(searchValue) || x.Reference.Contains(searchValue));

			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }

        public virtual int FindAvailableOrderListCount(List<Order> notAvailableList, string searchValue)
        {
            IEnumerable<int> notAvailableIDList = notAvailableList.Select(na => na.Id);
            return EntitiesDB.OrderSet.Where(a => !notAvailableIDList.Any(id => id == a.Id)).Where(x => string.IsNullOrEmpty(searchValue) || x.Reference.Contains(searchValue)).Count();
        }
	
		public virtual void AddOrderDetails(Order order, OrderDetail orderDetails)
        {
			order = Find(order.Id);
            orderDetails.Order = order;
            order.OrderDetails.Add(orderDetails);
            orderDetails.ProductDetail = EntitiesDB.EntryWithState(orderDetails.ProductDetail, EntityState.Unchanged);

            Change(order);
        }
		
		public virtual void RemoveOrderDetails(Order order, OrderDetail orderDetails)
        {
            order.OrderDetails.Remove(orderDetails);
			EntitiesDB.OrderDetailSet.Remove(orderDetails);			
            Change(order);
        }

    }
}
